package com.chaochis.springboottestdemo.base;

import com.chaochis.springboottestdemo.enumList.ApiCode;
import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class ApiResult {

  private Object result;
  private ApiCode code;
  private String msg;

  private ApiResult(Object result, ApiCode apiCode, String msg) {
    this.result = result;
    this.code = apiCode;
    this.msg = msg;
  }

  public static ApiResult ok() {
    return ok(null);
  }

  public static ApiResult ok(Object result) {
    return new ApiResult(result, ApiCode.Success, null);
  }

  public static ApiResult fail() {
    return new ApiResult(null, ApiCode.Fail, null);
  }

  public static ApiResult fail(Exception e) {
    return new ApiResult(null, ApiCode.Fail, e.getMessage());
  }

  public static ApiResult fail(String msg) {
    return new ApiResult(null, ApiCode.Fail, msg);
  }

  public static ApiResult build(Object result, ApiCode code, String msg) {
    return new ApiResult(result, code, msg);
  }

  public static ApiResult build(Object result, ApiCode code, Exception e) {
    return new ApiResult(result, code, e.getMessage());
  }

}

